<?php
// 设置页面编码
header('Content-Type: text/html; charset=utf-8');

// 获取用户输入的序列号
$sn = isset($_POST['sn']) ? trim($_POST['sn']) : '';

// 初始化状态和错误信息
$status = '';
$error = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (empty($sn)) {
        $error = '请输入序列号';
    } else {
        // 检查序列号是否包含非法字符
        if (preg_match('/[\/\\\\]/', $sn)) {
            $error = '序列号包含非法字符';
        } else {
            $userDir = __DIR__ . '/user/' . $sn;
            $logPath = $userDir . '/service_sh_access.log';
            
            // 先检查用户目录是否存在
            if (!is_dir($userDir)) {
                $error = '设备未注册/不存在';
            } elseif (file_exists($logPath)) {
                $modifiedTime = filemtime($logPath);
                $currentTime = time();
                $timeDiff = $currentTime - $modifiedTime;
                
                // 格式化时间显示
                $lastActivityTime = date('Y-m-d H:i:s', $modifiedTime);
                
                if ($timeDiff < 180) { // 180秒 = 3分钟
                    $status = '在线状态: <span style="color: green;">在线</span><br>';
                    $status .= '上次心跳时间: ' . $lastActivityTime;
                } else {
                    $status = '在线状态: <span style="color: red;">不在线</span><br>';
                    $status .= '最后在线时间: ' . $lastActivityTime;
                }
            } else {
                $error = '该设备没有日志文件';
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备状态查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .status {
            margin: 20px 0;
            padding: 10px;
            border: 1px solid #ddd;
        }
        .error {
            color: red;
        }
        .time-info {
            margin-top: 5px;
            color: #555;
        }
    </style>
</head>
<body>
    <h1>设备状态查询</h1>
    
    <form method="post">
        <div class="form-group">
            <label for="sn">请输入你的序列号(SN):</label>
            <input type="text" id="sn" name="sn" value="<?php echo htmlspecialchars($sn); ?>" required>
        </div>
        <button type="submit">查询</button>
    </form>
    
    <?php if (!empty($error)): ?>
        <div class="error"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <?php if (!empty($status)): ?>
        <div class="status">
            <?php echo $status; ?>
        </div>
    <?php endif; ?>
</body>
</html>